package com.imageim.yimirror.ui.activity;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.imageim.yimirror.R;
import com.imageim.yimirror.base.BaseActivity;
import com.imageim.yimirror.base.BaseFragment;
import com.imageim.yimirror.base.Constants;
import com.imageim.yimirror.common.ui.SceneManager;
import com.imageim.yimirror.common.ui.btn.BtnCircleClose;
import com.imageim.yimirror.common.ui.dialog.TipDialog;
import com.imageim.yimirror.common.util.AnimUtil;
import com.imageim.yimirror.common.util.UserUtil;
import com.imageim.yimirror.service.statistic.StatisticSDK;
import com.imageim.yimirror.service.statistic.StatisticUtil;
import com.imageim.yimirror.ui.adapter.SlideSwitchFragmentAdapter;
import com.imageim.yimirror.ui.fragment.BodyLineFragment;
import com.imageim.yimirror.ui.fragment.RadarFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Yuaihen.
 * on 2018/11/29
 * 雷达图和身线图Activity
 */
public class BodyAndRadarActivity extends BaseActivity {

    @BindView(R.id.viewPager)
    ViewPager mViewPager;
    @BindView(R.id.btn_close)
    BtnCircleClose mBtnClose;
    @BindView(R.id.ib_look_cloth)
    ImageView mIbLookCloth;
    @BindView(R.id.ib_restart_test)
    ImageView mIbRestartTest;
    private TipDialog mTipDialog;
    private SlideSwitchFragmentAdapter mFragmentAdapter;
    private BodyLineFragment mBodyLineFragment;
    private RadarFragment mRadarFragment;
    private List<BaseFragment> mFragmentList;
    private boolean flag;

    @Override
    protected void initBundle() {
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_body_and_radar;
    }


    @Override
    protected void initView() {
        ButterKnife.bind(this);
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void initListener() {
        super.initListener();
        //该用户第一次进入
        if (UserUtil.getTodayHasUse() == 1) {
            playTipAnim();
        }

        mBtnClose.setCloseImage(0, 300);
        mBtnClose.setOnTimeOverListener(new BtnCircleClose.OnTimeOverListener() {
            @Override
            public void onTimeOver() {
                finish();
            }
        });

        initViePageChangeListener();
        //        initViewPageTouchListener();
    }

    private void initViePageChangeListener() {
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 1) {
                    mBodyLineFragment.hideBodyLine();
                } else {
                    mBodyLineFragment.showBodyLine();
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                switch (state) {
                    case ViewPager.SCROLL_STATE_DRAGGING:
                        //拖的时候才进入下一页
                        flag = false;
                        break;
                    case ViewPager.SCROLL_STATE_SETTLING:
                        flag = true;
                        break;
                    case ViewPager.SCROLL_STATE_IDLE:
                        if (mViewPager.getCurrentItem() == mFragmentList.size() - 1 && !flag) {
                            SceneManager.toScene(mContext, MainActivity.class, null);
                            overridePendingTransition(R.anim.slide_in_right, R.anim.slide_in_left);
                            finish();
                        }
                        flag = true;
                        break;
                    default:
                        break;
                }
            }
        });
    }


    @Override
    protected void initData() {
        mFragmentList = new ArrayList<>();
        mBodyLineFragment = new BodyLineFragment();
        mRadarFragment = new RadarFragment();
        mFragmentList.add(mBodyLineFragment);
        mFragmentList.add(mRadarFragment);
        mFragmentAdapter = new SlideSwitchFragmentAdapter(getSupportFragmentManager(), mFragmentList);
        mViewPager.setAdapter(mFragmentAdapter);

        //进入页面
        StatisticSDK.startPage(StatisticUtil.getPageId(this.getClass().getName()), Constants.PageType.RADAR_AND_BODY_LINE);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mBtnClose != null) {
            mBtnClose.destory();
        }

        //离开页面
        StatisticSDK.endPage(StatisticUtil.getPageId(this.getClass().getName()), Constants.PageType.RADAR_AND_BODY_LINE);
    }

    /**
     * 用户第一次进入播放的手势动画
     */
    private void playTipAnim() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                startTips();
            }
        }, 2000);

    }

    private void startTips() {
        if (mTipDialog == null) {
            mTipDialog = new TipDialog(mContext) {
                @Override
                public int setLayoutId() {
                    return R.layout.dialog_layout_tips;
                }

                @Override
                protected void initView(View tipsView) {
                    RelativeLayout ivSlide = tipsView.findViewById(R.id.rl_slide);
                    startAnim(ivSlide);
                }
            };
        }
        mTipDialog.show();
    }


    private void startAnim(RelativeLayout ivSlide) {
        AnimUtil.gestureTipAnim(ivSlide);
    }


    @OnClick({R.id.ib_look_cloth, R.id.ib_restart_test})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ib_look_cloth:
                SceneManager.toScene(mContext, MainActivity.class, null);
                finish();
                break;
            case R.id.ib_restart_test:
                SceneManager.toScene(mContext, ScanFaceActivity.class, null);
                finish();
                break;
            default:
                break;
        }
    }

    private long lastTouchEventTime;

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (System.currentTimeMillis() - lastTouchEventTime > 1000 && mBtnClose.getCount() < 60) {
            lastTouchEventTime = System.currentTimeMillis();
            mBtnClose.restart(300);
        }
        return super.dispatchTouchEvent(ev);
    }
}
